package com.irdstudio.efp.nls.service.impl.dxm;


import java.math.BigDecimal;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import com.irdstudio.basic.framework.core.util.TimeUtil;
import com.irdstudio.efp.esb.common.constant.EsbBizEnums;
import com.irdstudio.efp.esb.common.constant.SDicClientConstant;
import com.irdstudio.efp.esb.common.constant.dxm.DxmEnum;
import com.irdstudio.efp.esb.common.util.SDicMappingClient;
import com.irdstudio.efp.esb.service.bo.req.dxm.ReqLoanAdjBizmessage;
import com.irdstudio.efp.esb.service.bo.req.dxm.ReqLoanAdjLmtNoticeBean;
import com.irdstudio.efp.esb.service.bo.resp.dxm.RespLoanAdjLmtNoticeBean;
import com.irdstudio.efp.esb.service.facade.dxm.LoanAdjLmtNoticeService;
import com.irdstudio.efp.limit.service.vo.LmtAdjustThirdVO;
import com.irdstudio.efp.nls.service.vo.queue.NlsProcessBizVO;

/**
  * 额度调整申请结果通知接口
  * @author  qiuyf
  * @since 2019年3月14日 上午11:17:49
  * @version 1.0
 */
@Service("lmtAdjCallBackService")
public class LmtAdjCallBackServiceImpl extends LimitSoltServiceImpl {

	private static Logger logger = LoggerFactory.getLogger(LmtAdjCallBackServiceImpl.class);
	
	@Autowired
	@Qualifier("loanAdjLmtNoticeService")
	private LoanAdjLmtNoticeService loanAdjLmtNoticeService;
	
	@Override
	protected void doExecute(NlsProcessBizVO nlsProcessBizVo, LmtAdjustThirdVO lmtAdjustThirdVo) throws Exception {
		String adjSeq = lmtAdjustThirdVo.getAdjSeq();
		String adjAmtId = lmtAdjustThirdVo.getAdjAmtId();
		logger.info("------------ 额度调整申请结果回调开始，会话ID为：" + adjSeq + "-----------" + "调额申请流水号（唯一标记一次调额）：" + adjAmtId);
		try {
			ReqLoanAdjLmtNoticeBean reqBean = new ReqLoanAdjLmtNoticeBean();
			/* 产品代码  */
			reqBean.setProdCd(SDicMappingClient.getExternalSysDicVal(SDicClientConstant.SYSID_IOP, SDicClientConstant.PRD_CODE, lmtAdjustThirdVo.getPrdId()));
			/* 返回代码  */
			reqBean.setRetCd(DxmEnum.RetCd.SUCCESS.getValue());
			/* 交易模式为“异步” */
			reqBean.setTxnMd(DxmEnum.TxnModel.SYNC.getValue());
			/* 请求标志  = 请求方需要保证永远唯一,响应方据此判断是否重复请求 */
			reqBean.setRepeatRqsBss(lmtAdjustThirdVo.getBdReqSn());
			/* 版本号 */
			reqBean.setVerNo(lmtAdjustThirdVo.getVerNo());
			/* 请求时间戳  */
			reqBean.setRqsTmstmp(TimeUtil.getTimeStampByPattern("yyyyMMddHHmmssSSS"));
			/* 会话ID */
			reqBean.setSessionId(adjSeq);
			/* 通知类型 */     
			reqBean.setAdvType(lmtAdjustThirdVo.getAdvType()+"Callback");

			/** 业务数据组装  start*/
			ReqLoanAdjBizmessage reqBizmessage = new ReqLoanAdjBizmessage();
			/* 机构原因码  */
			reqBizmessage.setInstRsnCd(EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE());
			/* 机构原因码说明  */
			reqBizmessage.setInstRsnCdRmk("调额申请通过！");
			/* 机构策略码 */
			reqBizmessage.setInstPolcyCd(DxmEnum.OrgStrategyCode.PASS_10000.getVALUE());
			/* 确认调额后的金额   */
			reqBizmessage.setCfrmAdjtAmt(lmtAdjustThirdVo.getAdjResultLmt().toString());
			/** 业务数据组装 end */ 
			reqBean.setBizmessage(reqBizmessage);
			
			RespLoanAdjLmtNoticeBean respBean = loanAdjLmtNoticeService.loanAdjLmtNotice(reqBean);
			
			Objects.requireNonNull(respBean);
			if(!EsbBizEnums.RetCodeEnum.SUCCESS.getVALUE().equals(respBean.getRetCd())) {
				logger.error("额度调整申请结果回调返回信息出错!");
			}
		} catch (Exception e) {
			logger.error("额度调整申请结果回调出现异常：" + e.getMessage());
		} finally {
			logger.info("------------ 额度调整申请结果回调开始，会话ID为：" + adjSeq + "-----------" + "调额申请流水号（唯一标记一次调额）：" + adjAmtId);
		}
	}

}
